#include "filecheckmanager.h"
#include <QCryptographicHash>

/**
 * @brief FileCheckManager::FileCheckManager
 * 检测下载文件是否是安全的，通过收集网络上已知病毒文件的md5值进行校验
 */
FileCheckManager *FileCheckManager::m_fileCheckManager = nullptr;

FileCheckManager::FileCheckManager()
{

}

FileCheckManager *FileCheckManager::instance()
{

    if(m_fileCheckManager == nullptr){
        m_fileCheckManager = new FileCheckManager();
    }
    return m_fileCheckManager;

}

bool FileCheckManager::check(QString filename)
{
    QFile theFile(filename);
    bool res = theFile.open(QIODevice::ReadOnly);
    QByteArray ba = QCryptographicHash::hash(theFile.readAll(), QCryptographicHash::Md5);
    theFile.close();
    QString newMd5 = ba.toHex().constData();
    QFile file(":/safe_explore_resource/not_safe_file_md5.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "open file error";
        return true;
    }
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        if(newMd5.contains(line)){
            return false;
        }
    }
    return true;
}


